$(function () {
    // 默认加载部门树
    vm.getDept();
    // 初始化表格
    $("#lineCoordinateTable").Grid({
        url: '../devlinedevcoordinate/list',
        colModel: [
            {label: '设备名称', name: 'name', index: "name", key: true},
            {label: '设备编码', name: 'code', index: "code"},
            {label: '报警时长阈值', name: 'alarmThreshold', index: "alarmThreshold"},
            {label: 'X坐标', name: 'pagex', index: "pagex"},
            {label: 'Y坐标', name: 'pagey', index: "pagey"},
            {label: '宽', name: 'width'},
            {label: '高', name: 'height'}
        ]
    });
});

var ztree;
//部门树
var deptSetting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "deptId",
            pIdKey: "parentId",
            rootPId: '01'
        },
        key: {
            url: "nourl",
            name: "name" //name 与 typeName 绑定
        },
    },
    callback: {
        beforeCheck: true,
        beforeClick: outTreeCheck
    }
};

function outTreeCheck(e, treeObj, treeNode) {
    //此处treeNode 为当前节点
    vm.q.lineCode = treeObj.deptId;
    // 执行查询
    vm.currentLevel = treeObj.deptLevel;
    vm.devLineDevCoordinate.code = treeObj.deptId; // 保存用
    // 设置回显
    vm.loadLine(function (res) {
        if(res.lineCoorSet){
            vm.showCoordinateSet = true;
            vm.lineCoorSet = res.lineCoorSet;
        }else{
            vm.showCoordinateSet = false;
        }
    });

    vm.query();
}

var ztree;

var vm = new Vue({
    el: '#sysDept',
    data: {
        showList: true, // 是否展示列表
        showCoordinateSet: true, // 是否显示坐标设置按钮
        lineCoorSet: '', // 点击左侧树之后获取到的服务器产线配置信息
        title: null,
        treeList: [],
        devLineDevCoordinate: {url:'', code:'', id:''},
        ruleValidate: {
            code: [
                {required: true, message: '生产线必选', trigger: 'blur'}
            ],
            name: [
                {required: true, message: '生产线必选', trigger: 'blur'}
            ],

        },
        q:{
            lineCode:'' // 产线code
        },
        currentLevel:0 // 当前选中的组织level
    },
    methods: {
        getDept: function () {
            //加载部门树
            Ajax.request({
                url: '../sys/user/treeDeptBySCXLevel?level=3',
                async: false,
                successCallback: function (r) {
                    ztree = $.fn.zTree.init($("#deptTree"), deptSetting, r.list);
                    ztree.expandAll(true);
                }
            });
        },
        setDevCoordinate: function(){
            // 设置产线二维平面图设备坐标
            var _this = this;
            if (this.q.lineCode == '' || this.currentLevel != 3) {
                iview.Message.error('请选择产线');
                return;
            }
            Ajax.request({
                url: "../devlinedevcoordinate/info?lineCode=" + _this.q.lineCode,
                async: true,
                successCallback: function (r) {
                    if(r.lineCoorSet){
                        window.location.href = '../analyse/linePlanSetting.html?lineCode=' + _this.q.lineCode;
                    }else{
                        iview.Message.error('请在设置平面图后再进行操作');
                    }
                }
            });
        },
        loadLine: function(callback){

            Ajax.request({
                url: "../devlinedevcoordinate/info?lineCode=" + this.q.lineCode,
                async: true,
                successCallback: function (r) {
                    callback(r);
                }
            });
        },
        getTreeList: function () {
            Ajax.request({
                url: '../sys/user/treeDeptBySCX',
                async: false,
                successCallback: function (r) {
                    vm.treeList = r.list;
                }
            });
        },
        add: function () {
            var _this = this;
            if (this.q.lineCode == '' || this.currentLevel != 3) {
                iview.Message.error('请选择产线');
                return;
            }
            Ajax.request({
                url: "../devlinedevcoordinate/info?lineCode=" + this.q.lineCode,
                async: true,
                successCallback: function (r) {
                    vm.devLineDevCoordinate = {code: '', url: ''};
                    if(r.lineCoorSet){
                        vm.title = "修改";
                        vm.devLineDevCoordinate = r.lineCoorSet;
                    }else{
                        vm.title = "新增";
                    }
                    vm.showList = false;
                }
            });

        },
        saveOrUpdate: function (event) {
            var url;
            if(vm.devLineDevCoordinate.id == null){
                url = "../devlinedevcoordinate/save";
                vm.devLineDevCoordinate.code = vm.q.lineCode;
            }else{
                url = "../devlinedevcoordinate/update";
            }

            Ajax.request({
                url: url,
                contentType: "application/json",
                params: JSON.stringify(vm.devLineDevCoordinate),
                type: 'POST',
                successCallback: function () {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        deptTree: function (param) {
            if (param === 'prod') {
                openWindow({
                    title: "选择生产线",
                    area: ['300px', '450px'],
                    content: jQuery("#categoryLayer"),
                    btn: ['确定', '取消'],
                    btn1: function (index) {
                        var node = ztree.getSelectedNodes();
                        if (node[0].deptType === 'DT_CJKSCX') {
                            var deptCode = node[0].deptId;
                            if(deptCode.length === 6){
                                vm.devLineDevCoordinate.code = deptCode;
                                vm.devLineDevCoordinate.name = node[0].name;
                                layer.close(index);
                            } else {
                                alert("请选择生产线");
                            }
                        } else {
                            alert("请选择生产线");
                        }
                    }
                });
            }
        },
        query: function () {
            vm.reload();
        },
        reload: function () {
            vm.showList = true;
            var page = $("#lineCoordinateTable").jqGrid('getGridParam', 'page');
            $("#lineCoordinateTable").jqGrid('setGridParam', {
                postData: vm.q,
                page: page
            }).trigger("reloadGrid");
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传格式正确的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccess: function (res, file) {
            this.$Loading.finish();
            iview.Message.success('文件上传成功');
            vm.devLineDevCoordinate.url = file.response.url;
        }
    }
});

function getDeptId() {
    var selected = $('#sysDeptGrid').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        iview.Message.error("请选择一条记录");
        return ;
    } else {
        return selected[0].id;
    }
}
